﻿using System;
using Microsoft.Extensions.Logging;
namespace ConsoleApp.Utils
{
    public  class LogUtils
    {
        private static readonly ILogger log = new LoggerFactory().CreateLogger("ConsoleApp"); 
        public static void Trace(object msg, Exception ex = null)
        {
            if (ex == null)
            {
                log.LogTrace(msg.ToString());
            }
            else
            {
                log.LogTrace(msg + GetExceptionMessage(ex));
            }
        }

        public static void Debug(object msg, Exception ex = null)
        {
            if (ex == null)
            {
                log.LogDebug(msg.ToString());
            }
            else
            {
                log.LogDebug(msg + GetExceptionMessage(ex));
            }
        }

        public static void Info(object msg, Exception ex = null)
        {
            if (ex == null)
            {
                log.LogInformation(msg.ToString());
            }
            else
            {
                log.LogInformation(msg + GetExceptionMessage(ex));
            }
        }

        public static void Warn(object msg, Exception ex = null)
        {
            if (ex == null)
            {
                log.LogWarning(msg.ToString());
            }
            else
            {
                log.LogWarning(msg + GetExceptionMessage(ex));
            }
        }

        public static void Error(object msg, Exception ex = null)
        {
            if (ex == null)
            {
                log.LogError(msg.ToString());
            }
            else
            {
                log.LogError(msg + GetExceptionMessage(ex));
            }
        }

        public static void Error(Exception ex)
        {
            if (ex != null)
            {
                log.LogError(GetExceptionMessage(ex));
            }
        }

        public static void Fatal(object msg, Exception ex = null)
        {
            if (ex == null)
            {
                log.LogCritical(msg.ToString());
            }
            else
            {
                log.LogCritical(msg + GetExceptionMessage(ex));
            }
        }

        public static void Fatal(Exception ex)
        {
            if (ex != null)
            {
                log.LogCritical(GetExceptionMessage(ex));
            }
        }

        private static string GetExceptionMessage(Exception ex)
        {
            string message = string.Empty;
            if (ex != null)
            {
                message += ex.Message;
                message += Environment.NewLine;
                Exception originalException = ex.GetOriginalException();
                if (originalException != null)
                {
                    if (originalException.Message != ex.Message)
                    {
                        message += originalException.Message;
                        message += Environment.NewLine;
                    }
                }
                message += ex.StackTrace;
                message += Environment.NewLine;
            }
            return message;
        } 
    }
    public static class ExceptionExtension
    {
        public static Exception GetOriginalException(this Exception ex)
        {
            if (ex.InnerException == null) return ex;

            return ex.InnerException.GetOriginalException();
        }
    }
}
